<?php
namespace components;

use libs\URIProcess;
use libs\Util;

class GlobalComponent extends Base
{
	const REQUEST_SUCCESS_CODE = 99999;

	private $start;
	private $end;
	private $requestObj;
	public function before($config){
		$this->start = Util::microtime_float();
		O::setConfig($config);
	}
	public function after(){
		$this->end = Util::microtime_float();
		Register::writer(
			'request.log.' . date('Y-m-d'),
			'domain:'       . URIProcess::getDomain(),
			'api:'		    . URIProcess::getRequestAPI(),
			'spendTime(ms):'. round(($this->end - $this->start),3),
			'params:'       . json_encode($this->requestObj->params),
			'method:'       . URIProcess::getRequestMethod()
		);
	}
	public function run($config){
		$this->before($config);

		list($controller,$action) = URIProcess::parseScenarioUrl();
		$this->requestObj = new $controller(URIProcess::parseRequestParams());
		$this->requestObj->paramCheck($action);
		$result = $this->requestObj->$action();
		$this->after();

		$this->send($result);
	}
	
	public function send(Array $data){
		\libs\URIProcess::dispatch([
			'code'    => self::REQUEST_SUCCESS_CODE,
			'message' => 'success',
			'data'    => $data,
		]);
	}
}